﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_useredit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["login"] == null)
            {
                Response.Redirect("login.aspx");
            }
            else
            {
                if (Request.QueryString["op"] == "e")
                {
                    YqhData obj = new YqhData();
                    btnup.Visible = false;
                    DataSet ds;
                    ds = obj.FillDataSet(string.Format("select * from userinfo where uid={0}", Request.QueryString["id"]));
                    txtname.Text = ds.Tables[0].Rows[0]["uname"].ToString();
                    txtpwd.Text = ds.Tables[0].Rows[0]["upwd"].ToString();
                    RadioButtonList1.SelectedValue = ds.Tables[0].Rows[0]["upower"].ToString();
                }
                else if (Request.QueryString["op"] == "new")
                {
                    btnsave.Visible = false;
                }
            }
        }
    }
    protected void btnok_Click(object sender, EventArgs e)
    {
        YqhData obj = new YqhData();
        string sql = string.Format(@"insert into userinfo 
                                    (uname,upwd,upower)
                                     values('{0}','{1}',{2})",
                                     txtname.Text,txtpwd.Text,RadioButtonList1.SelectedValue);
        int i = 0;
        try
        {
            i = obj.ExecuteSql(sql);
        }
        catch
        {
            i = 0;
        }

        if (i >= 1)
        {
            Response.Write("<script>alert('添加成功！');window.location.href = 'userlist.aspx';</script>");
        }
        else
        {
            obj.ShowMsg("添加失败！请重新添加！");
        }
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        string sql = string.Format(@"update userinfo 
                    set uname='{0}',upwd='{1}',upower={2}
                    where uid={3}", 
                    txtname.Text, txtpwd.Text, RadioButtonList1.SelectedValue, Request.QueryString["id"]);
        YqhData obj = new YqhData();
        int i = 0;
        try
        {
            i = obj.ExecuteSql(sql);
        }
        catch
        {
            i = 0;
        }

        if (i >= 1)
        {
            Response.Write("<script>alert('修改成功！');window.location.href = 'userlist.aspx';</script>");
        }
        else
        {
            obj.ShowMsg("修改失败！请重新修改！");
        }
    }
}
